package com.example.by.test;

import android.app.AlertDialog;
import android.content.Context;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by BY on 2016/4/21.
 */
public class Util {

    private static AlertDialog alertDialog;
    private static LayoutInflater inflater;

    public static void loadingAlert(Context context) {
        inflater = LayoutInflater.from(context);
        View view=inflater.inflate(R.layout.loading_view,null);
        alertDialog = new AlertDialog.Builder(context).create();
        TextView loadingText= (TextView) view.findViewById(R.id.loadingTaxt);
        ImageView imageView= (ImageView) view.findViewById(R.id.circle);
        MaterialProgressDrawable progress=new MaterialProgressDrawable(context,imageView);
        imageView.setImageDrawable(progress);
        progress.start();
        loadingText.setText("加载中...");
        alertDialog.setView(view);
        alertDialog.show();
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = manager.getDefaultDisplay();
        Window window = alertDialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = (int) (display.getWidth() * 0.3);
        window.setAttributes(lp);
    }

    public static void cancelLoadingAlert() {
        if (alertDialog != null) {
            alertDialog.dismiss();

        }
    }
}
